﻿/** \file
* \$Rev: 78 $
* 
* \$Date: 2011-03-31 15:21:30 +0000 (Thu, 31 Mar 2011) $
*
* \$URL: http://benfwaves.googlecode.com/svn/branches/fftw/BenfWaves.Library/PropertyExtractor.cs $
*/

using System;
using System.Globalization;
using System.Reflection;

namespace BenfWaves.Library
{
	/// <summary>
	/// A utility class that extracts property values from an object.
	/// </summary>
	public class PropertyExtractor
	{
		/// <summary>
		/// The character to separate properties in a property path.
		/// </summary>
		static readonly char[] propsplit = new char[] { '.' };

		/// <summary>
		/// Extract a property from an object.
		/// </summary>
		/// <typeparam name="SourceType">The type of the source object.</typeparam>
		/// <param name="proppath">The property path (SomeProp.SomeSubProp).</param>
		/// <param name="topSrc">The source object.</param>
		/// <returns>The retrieved property, as a string.</returns>
		static public string Extract<SourceType>(string proppath, SourceType topSrc)
		{
			Type topSrcType = typeof(SourceType);

			string[] props = proppath.Split(propsplit);
			object src = topSrc;
			PropertyInfo prop;
			Type srctype = topSrcType;

			// Loop through each portion of the property path (separated by '.')
			foreach (string propname in props)
			{
				if (src == null)
					return null;
				prop = srctype.GetProperty(propname);
				if (prop == null)
					return null;
				src = prop.GetValue(src, null);
				srctype = prop.PropertyType;
			}
			return src.ToString();
		}
	}
}
